package com.projet.whattowatch.activity;

import com.projet.whattowatch.R;
import com.projet.whattowatch.database.DataBase;
import com.projet.whattowatch.objet.LibCinema;
import com.projet.whattowatch.objet.LibFilm;
import com.projet.whattowatch.objet.LibHumain;
import com.projet.whattowatch.objet.LibUtilisateur;
import com.projet.whattowatch.objet.Utilisateur;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity
{
	private ImageView logo;
	private TextView ready;
	private EditText register, password;
	private Button logIn, signIn;
	public static DataBase app_db;
	private Object[] finalArray;
	public static LibUtilisateur mLibUtilisateur;
	public static LibFilm mLibFilm;
	public static LibHumain mLibHumain;
	public static LibCinema mLibCinema;
	
	final String EXTRA_EMAIL = "user_email";
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main_activity);

		app_db = new DataBase(getApplicationContext());
		

		finalArray = app_db.getAllMovieAndPerson();
		mLibFilm = (LibFilm)finalArray[0];
		mLibHumain = (LibHumain)finalArray[1];
		mLibUtilisateur = (LibUtilisateur)finalArray[2];
		mLibCinema = app_db.getAllCinema();
		
		logo = (ImageView)findViewById(R.id.logo);
		ready = (TextView)findViewById(R.id.readyTextView);
		register = (EditText)findViewById(R.id.emailEditText);
		password = (EditText)findViewById(R.id.passwordEditText);
		signIn = (Button)findViewById(R.id.signInButton);
		
		logIn = (Button)findViewById(R.id.logInButton);
		logIn.setOnClickListener(logInListener);
		
		signIn.setOnClickListener(signInListener);

		Animation animationLogo = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.anim_logo);
		logo.startAnimation(animationLogo);
		
		Animation animationBackground = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.anim_background);
		
		ready.startAnimation(animationBackground);
		register.startAnimation(animationBackground);
		password.startAnimation(animationBackground);
		logIn.startAnimation(animationBackground);
		signIn.startAnimation(animationBackground);
	}
	
	public OnClickListener logInListener = new OnClickListener()
    {
		@Override
		public void onClick(View arg0)
		{
			EditText email = (EditText) findViewById(R.id.emailEditText);
			String emailStr = email.getText().toString();
			EditText password = (EditText) findViewById(R.id.passwordEditText);
			String passwordStr = password.getText().toString();

			boolean exists = false;
			Utilisateur user = null;
			
			for (int i = 0 ; i < mLibUtilisateur.getlistUtilisateur().size() ; i++)
			{
				if (emailStr.equals(mLibUtilisateur.getlistUtilisateur().get(i).getAdresseMail()))
				{
					exists = true;
					user =  mLibUtilisateur.getlistUtilisateur().get(i);
				} 
			}
			if(exists)
			{
				if(passwordStr.equals(user.getPassword()))
				{
					Intent logInIntent = new Intent(getApplicationContext(),ConnectedActivity.class);
					logInIntent.putExtra(EXTRA_EMAIL, emailStr);
					startActivity(logInIntent);
				}
				else
				{
					Toast.makeText(getApplicationContext(), "Erreur : Mot de passe incorrect.",Toast.LENGTH_SHORT).show();
				}
			}
			else
			{
				Toast.makeText(getApplicationContext(), "Erreur : cette adresse e-mail n'est pas répertoriée!",Toast.LENGTH_SHORT).show();
			}
		}
    };
    
    public OnClickListener signInListener = new OnClickListener()
    {
		@Override
		public void onClick(View arg0)
		{
			Intent signInIntent = new Intent(getApplicationContext(), SignInActivity.class);
			startActivity(signInIntent);
		}
    };
}